package dna;




import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


import core.gui.FileChooser;
import core.inputoutput.formatting.Format;
import core.main.Main;
import dna.macromolecules.DNAStructure;


public class DNAtoCGLatticeMain extends Main {

	
	public static double[][] shifts;
	public static double shiftFactor;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PDBparserDNA parser = new PDBparserDNA();
		DNAshifter shifter = new DNAshifter(Format.DNA_ATOMISTIC);
		DNAtoCGconverter CG = new DNAtoCGconverter(Format.DNA_COARSE_GRAIN);
		
		File INPUT = initializeGUI("DNA PDB to Coarse Grain Lattice Maker", parser, shifter, CG);
		shiftFactor = Double.parseDouble(JOptionPane.showInputDialog("Enter a Shift Factor"));
		createShiftArrays(JOptionPane.showInputDialog("Enter lattice dimensions - W x H x D"));
		
		
	
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(
					makeOutputFileName(INPUT.getName()))),
						   out2 = new BufferedWriter(new FileWriter(
					makeCGFileName(makeOutputFileName(INPUT.getName()))));
			
			
			
			
			
			DNAStructure struct = parser.parse(INPUT, Format.DNA_ATOMISTIC);
			int n = 0;
				for(double dx: shifts[0]){
					for (double dy: shifts[1]){
						for (double dz: shifts[2]){
							
							myGUI.getOPpanel().print("Currenly Shifting by: (" + dx +","+ dy +","+ dz+")");
							out.write(shifter.applyConverter(struct,dx,dy,dz,n++));
						}
					}
				}
				
				out.close();
				myGUI.getOPpanel().print("Begin CG parse");
				out2.write(CG.applyConverter(parser.parse(new File(makeOutputFileName(INPUT.getName())), Format.DNA_ATOMISTIC))); 
				out2.close();
				myGUI.getOPpanel().print("Done!");
				
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private static void createShiftArrays(String string) {
		int[] dim = new int[]{Integer.parseInt(string.split("x")[0].trim()),
							  Integer.parseInt(string.split("x")[1].trim()),
							  Integer.parseInt(string.split("x")[2].trim())};
		shifts = new double[dim.length][];
		for (int i = 0; i < dim.length; i++){
			shifts[i] = createShiftArray(dim[i]);
		}
		
		}

	private static double[] createShiftArray(int dim) {
		double[] shiftN = new double[dim];
		for(int i = 0; i<shiftN.length; i++){
			shiftN[i] = i*shiftFactor;
		}
		return shiftN;
	}

	private static String makeCGFileName(String input) {
		if (input.contains("."))
			input = input.substring(0,input.lastIndexOf(".")) ;
		return input +"_CG.pdb";
	}
		

	private static String makeOutputFileName(String input) {
		if (input.contains("."))
			input = input.substring(0,input.lastIndexOf(".")) ;
		return input + "_Latticed("+ 
									(int)((shifts[0][shifts[0].length-1]/shiftFactor)+1) + "," + 
									(int)((shifts[1][shifts[1].length-1]/shiftFactor)+1) + "," + 
									(int)((shifts[2][shifts[2].length-1]/shiftFactor)+1) + ").pdb";
	}

}
